﻿using UnityEngine;
using System.Collections;

public class SpecialCheck : MonoBehaviour {
    public static SpecialCheck instance;
    private GameManager manager;
	// Use this for initialization
	void Start () {
        manager = GameManager.getInstance();
	}
    void Awake() {
        instance = this;
    }
    public void MatchCandyCondition()
    {
		if(manager.gamestate!=GameState.checking){
			return;
		}
		if((CandyNeighbor.instance.clicked_candy_==null)||(CandyNeighbor.instance.target_candy==null)){
			manager.gamestate=GameState.checkboardstate;
			CandyCheckMatch.instance.CheckBoardState();
			return;
		}
		if (CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "star")
        {
            CandyMatchedConditions.instance.SwitchStar(CandyNeighbor.instance.clicked_candy_, CandyNeighbor.instance.target_candy);
			CandyDemo.instance.move_number-=1;
            CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
        }
		else if (CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "star")
		{
			CandyDemo.instance.move_number-=1;
            CandyMatchedConditions.instance.SwitchStar(CandyNeighbor.instance.target_candy, CandyNeighbor.instance.clicked_candy_);
            CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
        }
		else if (((CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "vertical") || (CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "horizontal")) &&
		         ((CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "vertical") || (CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "horizontal")))
        {
			CandyDemo.instance.move_number-=1;
            CandyMatchedConditions.instance.VerticalORHorizontal(CandyNeighbor.instance.target_candy, CandyNeighbor.instance.clicked_candy_);
            CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
        }
		else if ((CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "special") && ((CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "vertical")
		                                                                                                       || (CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "horizontal")))
        {
			CandyDemo.instance.move_number-=1;
			CandyMatchedConditions.instance.SpecialAndHorizontal(CandyNeighbor.instance.target_candy, CandyNeighbor.instance.clicked_candy_);
            CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
		} else if ((CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "special") && ((CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "vertical")
		                                                                       || (CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "horizontal")))
		{
			CandyDemo.instance.move_number-=1;
			CandyMatchedConditions.instance.SpecialAndHorizontal(CandyNeighbor.instance.target_candy, CandyNeighbor.instance.clicked_candy_);
			CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
		}
		else if ((CandyNeighbor.instance.clicked_candy_.GetComponent<CandyController>().Type == "special") && (CandyNeighbor.instance.target_candy.GetComponent<CandyController>().Type == "special"))
        {	
			CandyDemo.instance.move_number-=1;
            CandyMatchedConditions.instance.SpecialAndSpecial(CandyNeighbor.instance.target_candy);
            CandyNeighbor.instance.clicked_candy_ = CandyNeighbor.instance.target_candy = null;
			manager.gamestate=GameState.destroy;
            CandyDrop.instance.Invoke("DroppingCandy", 0.1f);

        }
        else
        {
            CandyCheckMatch.instance.Checking(CandyNeighbor.instance.target_candy);
            CandyCheckMatch.instance.Checking(CandyNeighbor.instance.clicked_candy_);
			CandyDemo.instance.UIscore.AddScore(CandyCheckMatch.instance.simple_matched_candy_count*20);
			CandyDemo.instance.UIscore.AddScore(CandyCheckMatch.instance.special_matched_candy_count*40);
			CandyCheckMatch.instance.simple_matched_candy_count=CandyCheckMatch.instance.special_matched_candy_count=0;
            if (manager.IsMatch)
            {
				manager.gamestate=GameState.destroy;
				CandyDemo.instance.move_number-=1;
                CandyDrop.instance.DroppingCandy();
            }
            else
            {
				manager.gamestate=GameState.moving;
                CandyDrop.instance.KinematicCandy(true);
                CandyMovement.instance.MoveBack();
            } 
        }
		CandyDemo.instance.MoveN.GetComponent<tk2dTextMesh>().text = CandyDemo.instance.move_number.ToString();
    }
}
